package com.hcb.micro.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import com.hcb.micro.dto.QqbsAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hcb.micro.controller.base.BaseControllers;
import com.hcb.micro.dto.Manager;
import com.hcb.micro.dto.QqbsFans;
import com.hcb.micro.service.IQqbsAccountService;
import com.hcb.micro.service.IQqbsFansService;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("qqbsFans")
public class QqbsFansController extends BaseControllers{
	
	@Autowired
	IQqbsFansService qqbsFansService;
	
	@Autowired
	IQqbsAccountService qqbsAccountService;
	
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public String list() {
		JSONObject json = new JSONObject();
		if (sign == 1) {
			json.put("result", "1");
			json.put("description", "请检查参数格式是否正确或者参数是否完整");
			return buildReqJsonInteger(1, json);
		}
		// 登录认证失败
		if (sign == 2) {
			json.put("result", "2");
			json.put("description", "账号未登录");
			return buildReqJsonInteger(2, json);
		}
		
		HttpSession session = request.getSession();
		Manager manager = (Manager) session.getAttribute("MANAGER");
        if(manager == null){
        	json.put("result", "1");
			json.put("description", "账号未登陆");
			return buildReqJsonObject(json);
        }
		
		JSONObject bodyInfo = JSONObject.fromObject(bodyString);
		
		Map<String,Object> para = new HashMap<>();
		if (bodyInfo.get("fansNickname") != null && !"".equals(bodyInfo.get("fansNickname"))) {
			para.put("fansNickname", bodyInfo.getString("fansNickname"));
		}
		if(bodyInfo.get("accountId") != null && !"".equals(bodyInfo.get("accountId"))) {
			para.put("accountId", bodyInfo.getString("accountId"));
		}
		if(bodyInfo.get("fansAccountId") != null && !"".equals(bodyInfo.get("fansAccountId"))) {
			para.put("fansAccountId", bodyInfo.getString("fansAccountId"));
		}
		if(bodyInfo.get("level") != null && !"".equals(bodyInfo.get("level"))) {
			para.put("level", bodyInfo.getString("level"));
		}
		
		List<QqbsFans> qfListAll = qqbsFansService.selectByPara(para);
		
		if(bodyInfo.get("page") != null && !"".equals(bodyInfo.get("page"))) {
			para.put("start", (Integer.valueOf(bodyInfo.getString("page"))-1)*Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		if(bodyInfo.get("pageSize") != null && !"".equals(bodyInfo.get("pageSize"))) {
			para.put("size", Integer.valueOf(bodyInfo.getString("pageSize")));
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<QqbsFans> qfList = qqbsFansService.selectByPara(para);
		
		if(qfList.size() <= 0){
			json.put("result", "1");
			json.put("description", "粉丝列表不存在");
			return buildReqJsonObject(json);
		}
		List<Map<String,String>> list = new ArrayList<>();
		for(QqbsFans qqbsAccount:qfList){
			Map<String,String> map = new HashMap<>();
			
			QqbsAccount qa = qqbsAccountService.selectByAccountId(qqbsAccount.getAccountId());
			
			map.put("id", qqbsAccount.getId()+"");
			map.put("accountId", qqbsAccount.getAccountId()+"");
			map.put("nickname", qa.getNickname()==null?"":qa.getNickname());
			map.put("image", qa.getImage()==null?"":qa.getImage());
			map.put("fansAccountId", qqbsAccount.getFansAccountId()+"");
			map.put("fansNickname", qqbsAccount.getFansNickname()==null?"":qqbsAccount.getFansNickname());
			map.put("fansImage", qqbsAccount.getFansImage()==null?"":qqbsAccount.getFansImage());
			map.put("level", qqbsAccount.getLevel()+"");
			map.put("createTime", sdf.format(qqbsAccount.getCreateTime()));
			list.add(map);
		}
		
    	json.put("result", "0");
		json.put("description", "获取粉丝列表成功");
		json.put("list", list);
		json.put("count", qfListAll.size());
		return buildReqJsonObject(json);
	}
}
